package DBTasks;

import java.sql.*;

public class DBTask {

    protected static Connection con = null;

    //Call this method before any other tasks
    public static void connectToDatabase() {
        if (con == null) {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con = DriverManager
                        .getConnection(
                                "jdbc:mysql://academic-mysql.cc.gatech.edu/cs4400_Team_11",
                                "cs4400_Team_11", "w01goZoX");
                if (!con.isClosed())
                    System.out.println("Successfully connected to "
                            + "MySQL server using TCP/IP...");
            }
            catch (Exception e) {
                System.err.println("Exception: " + e.getMessage());
            }
        }
    }

    //Example of how to connect to a database with JDBC
    public static void main(String[] args) {
        try {
            DBTask.connectToDatabase();
            java.sql.Statement statement = DBTask.con.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM Employee");
            while (rs.next()) {
                Integer id = rs.getInt("Employee_Num");
                String name = rs.getString("Shelter_Name");
                System.out.println("id " + id + " : name " + name);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
